import scrapy
import time
import re
import json
#from jd.items import JdItem
from ..items import JdItem
class JingSpider(scrapy.Spider):
    name = 'jing'
    #allowed_domains = ['jd.com']
    #start_urls = ['http://jd.com/']
    def start_requests(self):
        name = ['戴尔（DELL）','联想（Lenovo）','华为（HUAWEI）','ThinkPad','惠普（HP）','小米（MI）','荣耀（honor）','机械革命（MECHREVO）','神舟（HASEE）']
        name1 = ['神舟（HASEE）']

        for i in name:
            page = 3
            s = 51
            for x in range(1,100):
                page += 2
                s += 50
                url = 'https://search.jd.com/search?keyword=%E7%AC%94%E8%AE%B0%E6%9C%AC%E7%94%B5%E8%84%91&suggest=1.def.0.0&wq=%E7%AC%94%E8%AE%B0%E6%9C%AC%E7%94%B5%E8%84%91&ev=exbrand_'+i+'%5E&page='+str(page)+'&s='+str(s)+'&click=0'
                time.sleep(2)
                yield scrapy.Request(url=url, callback=self.chuli)

    def chuli(self,response):
        url = response.xpath('//*[@id="J_goodsList"]/ul/li/div/div/a/@href').getall()
        list1 = []
        for i in url:
            if i[:6] =='//item':
                if i not in  list1:
                    list1.append(i)
        for x in list1:
            re1 = re.findall('/(.*?)\.', x)
            yield  scrapy.Request(url='https://p.3.cn/prices/mgets?callback=jQuery9458813&type=1&area=13_2900_3533_0&skuIds=J_'+str(re1[1])+'&pdbp=0&pdtk=&pdpin=&pin=&pduid=2011763846&ext=11101100111&source=search_pc&_=1601886177421',meta={'ls':x,'Id':str(re1[1])},callback=self.jiage)
    def jiage(self,id):

        jds = json.loads(id.text.lstrip('jQuery9458813(').rstrip(');').replace(');', ''))
        #print('******************************')
        yield scrapy.Request(url='https:'+id.meta['ls'],meta={'jia':jds[0].get('p'),'Id':id.meta['Id']},callback=self.parse)

    def parse(self,r):
        jd = JdItem()
        jd['name'] = "".join(re.findall('商品名称：(.*?)</li>', r.text))
        jd['memory'] = "".join(re.findall('内存容量：(.*?)</li>', r.text))
        jd['cpu'] = "".join(re.findall('处理器：(.*?)</li>', r.text))
        jd['storage'] = "".join(re.findall('硬盘容量：(.*?)</li>', r.text))
        jd['size'] = "".join(re.findall('屏幕尺寸：(.*?)</li>', r.text))
        jd['model'] = "".join(re.findall('显卡型号：(.*?)</li>', r.text))
        jd['jiage'] =r.meta['jia']
        jd['uid'] = r.meta['Id']
        print('******************************')
        print(jd['name'])

        yield jd

'ev=exbrand_戴尔（DELL）%5E'
'ev=exbrand_联想（Lenovo）%5E'
'ev=exbrand_华为（HUAWEI）%5E'
'ev=exbrand_ThinkPad%5E'
'ev=exbrand_惠普（HP）%5E'
'ev=exbrand_小米（MI）%5E'
'ev=exbrand_荣耀（honor）%5E'
'ev=exbrand_机械革命（MECHREVO）%5E'
'ev=exbrand_神舟（HASEE）%5E'